package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.OaOtheraPurchase;
import com.ruoyi.system.domain.OaPaySum;
import com.ruoyi.system.domain.vo.OaPaySumVo;
import com.ruoyi.system.service.IOaOtheraPurchaseService;
import com.ruoyi.system.service.IOaPaySumService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.OaSupplierPayableSummary;
import com.ruoyi.system.service.IOaSupplierPayableSummaryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 供应商应付汇总Controller
 *
 * @author ruoyi
 * @date 2025-03-13
 */
@RestController
@RequestMapping("/system/summary")
@Transactional
public class OaSupplierPayableSummaryController extends BaseController {
    @Autowired
    private IOaSupplierPayableSummaryService oaSupplierPayableSummaryService;
    @Autowired
    private IOaPaySumService oaPaySumService;
    @Autowired
    private IOaOtheraPurchaseService oaOtheraPurchaseService;
    /**
     * 查询供应商应付汇总列表
     */
    @PreAuthorize("@ss.hasPermi('system:summary:list')")
    @GetMapping("/list")
    public TableDataInfo list(OaSupplierPayableSummary oaSupplierPayableSummary) {
        startPage();
        List<OaSupplierPayableSummary> list = oaSupplierPayableSummaryService.selectOaSupplierPayableSummaryList(oaSupplierPayableSummary);
        return getDataTable(list);
    }

    /**
     * 导出供应商应付汇总列表
     */
    @PreAuthorize("@ss.hasPermi('system:summary:export')")
    @Log(title = "供应商应付汇总", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OaSupplierPayableSummary oaSupplierPayableSummary) {
        List<OaSupplierPayableSummary> list = oaSupplierPayableSummaryService.selectOaSupplierPayableSummaryList(oaSupplierPayableSummary);
        ExcelUtil<OaSupplierPayableSummary> util = new ExcelUtil<OaSupplierPayableSummary>(OaSupplierPayableSummary.class);
        util.exportExcel(response, list, "供应商应付汇总数据");
    }

    /**
     * 获取供应商应付汇总详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:summary:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(oaSupplierPayableSummaryService.selectOaSupplierPayableSummaryById(id));
    }

    /**
     * 新增供应商应付汇总
     */
    @PreAuthorize("@ss.hasPermi('system:summary:add')")
    @Log(title = "供应商应付汇总", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OaSupplierPayableSummary oaSupplierPayableSummary) {
        return toAjax(oaSupplierPayableSummaryService.insertOaSupplierPayableSummary(oaSupplierPayableSummary));
    }

    /**
     * 修改供应商应付汇总
     */
    @PreAuthorize("@ss.hasPermi('system:summary:edit')")
    @Log(title = "供应商应付汇总", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OaSupplierPayableSummary oaSupplierPayableSummary) {
        return toAjax(oaSupplierPayableSummaryService.updateOaSupplierPayableSummary(oaSupplierPayableSummary));
    }

    /**
     * 删除供应商应付汇总
     */
    @PreAuthorize("@ss.hasPermi('system:summary:remove')")
    @Log(title = "供应商应付汇总", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(oaSupplierPayableSummaryService.deleteOaSupplierPayableSummaryByIds(ids));
    }

    @GetMapping("/list1")
    public TableDataInfo list1(String id) {
        ArrayList<OaSupplierPayableSummary> oaSupplierPayableSummaries = new ArrayList<>();
        OaSupplierPayableSummary oaSupplierPayableSummary = new OaSupplierPayableSummary();
        oaSupplierPayableSummary.setIsNumber(0L);
//        oaSupplierPayableSummary.setOtheraPurchaseId(id);
        List<OaSupplierPayableSummary> list = oaSupplierPayableSummaryService.selectOaSupplierPayableSummaryList(oaSupplierPayableSummary);
        for (OaSupplierPayableSummary summary : list) {
            for (String s : summary.getOtheraPurchaseId().split(",")) {
                if (s.equals(id)) {
                    oaSupplierPayableSummaries.add(summary);
                }
            }

        }
        return getDataTable(oaSupplierPayableSummaries);
    }

    @GetMapping("/list2")
    public TableDataInfo list2(String id) {
        ArrayList<OaSupplierPayableSummary> oaSupplierPayableSummaries = new ArrayList<>();
        OaSupplierPayableSummary oaSupplierPayableSummary = new OaSupplierPayableSummary();
        oaSupplierPayableSummary.setIsNumber(1L);
//        oaSupplierPayableSummary.setOtheraPurchaseId(id);
        List<OaSupplierPayableSummary> list = oaSupplierPayableSummaryService.selectOaSupplierPayableSummaryList(oaSupplierPayableSummary);
        for (OaSupplierPayableSummary summary : list) {
            for (String s : summary.getOtheraPurchaseId().split(",")) {
                if (s.equals(id)) {
                    oaSupplierPayableSummaries.add(summary);
                }
            }
        }
        return getDataTable(oaSupplierPayableSummaries);
    }

    @GetMapping("/list3")
    public OaPaySumVo list3(Long id, String supplierName) {
        // 创建一个OaSupplierPayableSummary对象
        OaSupplierPayableSummary oaSupplierPayableSummary = new OaSupplierPayableSummary();
        // 设置initiationId属性为传入的id
        oaSupplierPayableSummary.setInitiationId(id);
        // 设置supplierName属性为传入的supplierName
        oaSupplierPayableSummary.setSupplierName(supplierName);
        // 根据oaSupplierPayableSummary对象查询OaSupplierPayableSummary列表
        List<OaSupplierPayableSummary> list = oaSupplierPayableSummaryService.selectOaSupplierPayableSummaryList1(oaSupplierPayableSummary);
        // 如果查询结果大于1，抛出异常
        // 创建一个OaPaySumVo对象
        OaPaySumVo oaPaySumVo = new OaPaySumVo();
        List<OaPaySum> allPaySums = new ArrayList<>();
        // 遍历查询结果
        for (OaSupplierPayableSummary summary : list) {
            // 创建一个OaPaySum对象
            OaPaySum oaPaySum = new OaPaySum();
            // 设置paybleSummaryId属性为summary的id
            oaPaySum.setPaybleSummaryId(summary.getId());
            // 根据oaPaySum对象查询OaPaySum列表
            List<OaPaySum> oaPaySums = oaPaySumService.selectOaPaySumList(oaPaySum);
            // 设置oaPaySumVo的oaPaySums属性为查询结果
            allPaySums.addAll(oaPaySums);
        }
        oaPaySumVo.setOaPaySums(allPaySums);
        // 创建一个OaOtheraPurchase列表
        ArrayList<OaOtheraPurchase> oaOtheraPurchases = new ArrayList<>();
        // 获取查询结果的第一个元素的otheraPurchaseId
        String otheraPurchaseId = list.get(0).getOtheraPurchaseId();
        // 将otheraPurchaseId按照逗号分隔
        String[] split = otheraPurchaseId.split(",");
        // 遍历分隔结果
        for (String s : split) {
            // 根据分隔结果查询OaOtheraPurchase
            OaOtheraPurchase oaOtheraPurchase = oaOtheraPurchaseService.selectOaOtheraPurchaseById(Long.parseLong(s));
            // 将查询结果添加到oaOtheraPurchases列表中
            oaOtheraPurchases.add(oaOtheraPurchase);
        }
        // 设置oaPaySumVo的oaSupplierPayableSummary属性为查询结果的第一个元素
        oaPaySumVo.setOaSupplierPayableSummary(list.get(0));
        // 设置oaPaySumVo的oaOtheraPurchases属性为oaOtheraPurchases列表
        oaPaySumVo.setOaOtheraPurchases(oaOtheraPurchases);
        for (OaPaySum oaPaySum : oaPaySumVo.getOaPaySums()) {
            System.out.println(oaPaySum);
        }

        // 返回oaPaySumVo对象
        return oaPaySumVo;
    }

    @GetMapping("/list4")
    public OaPaySumVo list4(Long id, String supplierName) {
        // 创建一个OaSupplierPayableSummary对象
        OaSupplierPayableSummary oaSupplierPayableSummary = new OaSupplierPayableSummary();
        // 设置isNumber为1
        oaSupplierPayableSummary.setIsNumber(1L);
        // 设置initiationId为传入的id
        oaSupplierPayableSummary.setInitiationId(id);
        // 设置supplierName为传入的supplierName
        oaSupplierPayableSummary.setSupplierName(supplierName);
        // 根据oaSupplierPayableSummary查询出对应的列表
        List<OaSupplierPayableSummary> list = oaSupplierPayableSummaryService.selectOaSupplierPayableSummaryList(oaSupplierPayableSummary);
        // 如果列表长度大于1，抛出异常
        if (list.size() > 1) {
            throw new RuntimeException("重复记录");
        }
        // 创建一个OaPaySumVo对象
        OaPaySumVo oaPaySumVo = new OaPaySumVo();
        // 遍历列表
        for (OaSupplierPayableSummary summary : list) {
            // 创建一个OaPaySum对象
            OaPaySum oaPaySum = new OaPaySum();
            // 设置paybleSummaryId为summary的id
            oaPaySum.setPaybleSummaryId(summary.getId());
            // 根据oaPaySum查询出对应的列表
            List<OaPaySum> oaPaySums = oaPaySumService.selectOaPaySumList(oaPaySum);
            // 将oaPaySums设置到oaPaySumVo中
            oaPaySumVo.setOaPaySums(oaPaySums);
        }

        // 创建一个OaOtheraPurchase的列表
        ArrayList<OaOtheraPurchase> oaOtheraPurchases = new ArrayList<>();
        // 获取otheraPurchaseId
        String otheraPurchaseId = list.get(0).getOtheraPurchaseId();
        // 将otheraPurchaseId按照逗号分隔
        String[] split = otheraPurchaseId.split(",");
        // 遍历分隔后的字符串数组
        for (String s : split) {
            // 根据s查询出对应的OaOtheraPurchase对象
            OaOtheraPurchase oaOtheraPurchase = oaOtheraPurchaseService.selectOaOtheraPurchaseById(Long.parseLong(s));
            // 将oaOtheraPurchase添加到oaOtheraPurchases列表中
            oaOtheraPurchases.add(oaOtheraPurchase);
        }
        // 将list中的第一个元素设置到oaPaySumVo中
        oaPaySumVo.setOaSupplierPayableSummary(list.get(0));
        // 将oaOtheraPurchases设置到oaPaySumVo中
        oaPaySumVo.setOaOtheraPurchases(oaOtheraPurchases);
        // 返回oaPaySumVo
        return oaPaySumVo;
    }
}
